package views;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.Toast;

import com.ma.R;


public class WritePadDialog extends Dialog {
	private Context mContext;
	private WriteDialogListener mWriteDialogListener;
	private  PaintView mPaintView;
	private FrameLayout mFrameLayout;
	private Button mBtnOK, mBtnClear;
	ImageView mClose;

	public WritePadDialog(Context context,
			WriteDialogListener writeDialogListener) {
		super(context);
		this.mContext = context;
		this.mWriteDialogListener = writeDialogListener;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE); //无标题
		setContentView(R.layout.write_pad);
		setCanceledOnTouchOutside(false);
		mFrameLayout = (FrameLayout) findViewById(R.id.tablet_view);

		Window window =   getWindow();
		WindowManager.LayoutParams lp = window.getAttributes();
		lp.width = WindowManager.LayoutParams.MATCH_PARENT;   //设置宽度充满屏幕
		lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
		window.setAttributes(lp);
		// 获取屏幕尺寸
		DisplayMetrics mDisplayMetrics = new DisplayMetrics();
		window.getWindowManager().getDefaultDisplay().getMetrics(mDisplayMetrics);
		int screenWidth = mDisplayMetrics.widthPixels;
		int screenHeight = mDisplayMetrics.heightPixels;
		mPaintView = new PaintView(mContext, screenWidth, screenHeight);
		mFrameLayout.addView(mPaintView);
		mPaintView.requestFocus();

		mBtnOK = (Button) findViewById(R.id.yes);
		mBtnOK.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (mPaintView.getPath().isEmpty()) {
					Toast.makeText(mContext, "请写下你的大名", Toast.LENGTH_SHORT).show();
					return;
				}
				mWriteDialogListener.onPaintDone(mPaintView.getPaintBitmap());
				dismiss();
			}
		});

		mBtnClear = (Button) findViewById(R.id.no);
		mBtnClear.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				mPaintView.clear();
			}
		});

		mClose = (ImageView) findViewById(R.id.close);
		mClose.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				dismiss();
			}
		});
	}
}